<?php
/**
 * Created by PhpStorm.
 * User: 123
 * Date: 2020/4/18
 * Time: 20:11
 */
namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\library\Page;

/**
 * 时间轴控制器
 * Class Timeline
 * @package app\index\controller
 */
class Timeline extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        $cpage=!empty($this->request->param('cpage'))?intval($this->request->param('cpage')):1;
        $total=!empty($this->request->param('total'))?intval($this->request->param('total')):50;
        $time=!empty($this->request->param('time'))?trim($this->request->param('time')):0;
        $searchtime=!empty($this->request->param('searchtime'))?trim($this->request->param('searchtime')):0;

        $pageInfo=db('navigation')->where('controllername="'.$this->identify.'"')->field('id,title,keywords,description')->find();
        if(empty($pageInfo)) $this->error('导航不存在！',url('/index'));

        //站长推荐
        $recommendArticleList=db('article')->where('isrecommend=1')->order('sort desc,id desc')->field('id,title')->limit(8)->select();

        //合作伙伴
        $cooperativePartnerList=db('cooperative_partner')->where('type=1')->order('sort desc,id desc')->field('name,thumb,link')->limit(8)->select();
        $this->assign('recommendArticleList',$recommendArticleList);
        $this->assign('cooperativePartnerList',$cooperativePartnerList);

        $start=($cpage-1)*$total;
        $url='/timeline/'.$time.'/'.$searchtime;

        $where='1=1';

        if(!empty($searchtime)){
            $where.=' and from_unixtime(createtime,"%Y-%m-%d")="'.$searchtime.'"';
        }

        if(!empty($time)){
            $timeArr=explode(' - ',$time);
            $where.=" and createtime>=".strtotime(trim($timeArr[0]))." and createtime<=".strtotime(trim($timeArr[1]));
        }

        //文章里列表
        $articleList=db('article')->where($where)->order('createtime desc,sort desc,id desc')->field('id,title,from_unixtime(createtime,"%Y-%m-%d") as createtime')->limit($start,$total)->select();
        if(!empty($articleList)){
            $count=db('article')->where($where)->order('createtime desc,sort desc,id desc')->count();

            //分页
            $page=new Page();
            $show=$page->create($count,$cpage,$total,$url);

            $this->assign('articleList',$articleList);
            $this->assign('show',$show);
        }

        //历史上的今天
        $historyArticle=db('article')->where('from_unixtime(createtime,"%m-%d")="'.date('m-d').'" and from_unixtime(createtime,"%Y-%m-%d")!="'.date('Y-m-d').'"')->order('sort desc,id desc')->limit(8)->field('id,title')->select();

        //获得用户ip
        $ip=$this->request->ip();

        //根据ip是否存在用户信息
        $user=db('user')->where('joinip="'.$ip.'"')->field('slogan')->find();
        //用户不存在添加用户
        if(empty($user)){
            $defaultGroup=model('user_group')->where('status="normal" and name="默认组"')->field('id')->find();
            $userData['group_id']=!empty($defaultGroup)?$defaultGroup['id']:0;
            $userData['joinip']=$ip;
            $userData['jointime']=time();
            $userData['status']='normal';
            model('user')->allowField(true)->save($userData)?$user=db('user')->where('joinip="'.$ip.'"')->field('slogan')->find():$this->error('系统异常！');
        }

        $this->assign('recommendArticleList',$recommendArticleList);
        $this->assign('cooperativePartnerList',$cooperativePartnerList);
        $this->assign('historyArticle',$historyArticle);
        $this->assign('user',$user);
        $this->assign('pageInfo',$pageInfo);
        if(!empty($time)){
            $this->assign('time',$time);
        }
        if(!empty($searchtime)){
            $this->assign('searchtime',$searchtime);
        }
        return $this->view->fetch();
    }

}